//
//  AskRateApp.m
//  PriceOnline
//
//  Created by Le Anh Tai on 8/26/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "AskRateApp.h"

#define NUMBER_OF_OPEN_APP_FOR_ASK 3
#define NUMBER_OF_REJECT_RATE_FOR_DISABLE 3


@implementation AskRateApp

- (void)increaseCountOpenApp
{
    NSNumber *countOpenApp = [[NSUserDefaults standardUserDefaults] objectForKey:@"countOpenApp"];
    if (countOpenApp == nil)
    {
        countOpenApp = [NSNumber numberWithInt:0];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:countOpenApp.integerValue + 1] forKey:@"countOpenApp"];
    
    NSLog(@"increase count of open app");
}

- (void)increaseRejectRateApp
{
    NSNumber *countRejectRate = [[NSUserDefaults standardUserDefaults] objectForKey:@"countRejectRate"];
    if (countRejectRate == nil)
    {
        countRejectRate = [NSNumber numberWithInt:0];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:countRejectRate.integerValue + 1] forKey:@"countRejectRate"];
    
    NSLog(@"increase reject rate");
}

- (void)askLikeApp
{
    NSNumber *countOpenApp = [[NSUserDefaults standardUserDefaults] objectForKey:@"countOpenApp"];
    NSNumber *countRejectRate = [[NSUserDefaults standardUserDefaults] objectForKey:@"countRejectRate"];
    if (countOpenApp && countOpenApp.integerValue > NUMBER_OF_OPEN_APP_FOR_ASK && countRejectRate.integerValue <= NUMBER_OF_REJECT_RATE_FOR_DISABLE)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Nếu bạn thấy ứng dụng này hữu ích. Vui lòng Comment để chúng tôi phát triển nó tốt hơn." delegate:self cancelButtonTitle:@"Để sau" otherButtonTitles:@"Rate", nil];
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //cancel
    if (buttonIndex == 0)
    {
        [self increaseRejectRateApp];
        
        //reset count of ask rate
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:0] forKey:@"countOpenApp"];
        
        NSLog(@"Reject rate");
    }
    else
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.com/apps/priceonline"]];
        
        //disable ask rate
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:NUMBER_OF_REJECT_RATE_FOR_DISABLE + 1] forKey:@"countRejectRate"];
        
        NSLog(@"Accept rate");
    }
}

@end
